<?php

class BaseController extends Controller {

    protected $pageSize = 10; 
    protected function __construct() {
        $this->setLanguage();
    	$this->language_id = Session::get('language_id');
    	$this->language_code = Session::get('language_code');
    }
    
	/**
	 * Setup the layout used by the controller.
	 *
	 * @return void
	 */
	protected function setupLayout()
	{
		if ( ! is_null($this->layout))
		{
			$this->layout = View::make($this->layout);
		}
	}
    

    /**
     * Setup layout for ajax html response
     * @author Thuan Truong
     * @param View $view
     * @param string $title
     * @return Response
     */
    protected function ajaxResponse($view, $title = null)
    {
        $layout = View::make('layouts.ajax');
        $layout->content = $view;

        $response = Response::make($layout);
        $response->header('Cache-Control', 'nocache, no-store, max-age=0, must-revalidate');
        $response->header('Pragma', 'no-cache');
        $response->header('Expires', 'Fri, 01 Jan 1990 00:00:00 GMT');
        if (!empty($title)) {
            $response->header('X-Page-Title', "Mulberry Lane | $title");
        }

        return $response;
    }

	/**
	 *  Set language base on language_id session
	 *  If not existed then get first language (vi)
	 */
	protected  function setLanguage() {
		Session::put('language_id', Session::get('language_id'));
		Session::put('language_code', Session::get('language_code'));
		
		// Set language
		App::setLocale(Session::get('language_code'));
	}
    
    /**
     * check user is login
     * @author Thuan Truong
     * @return bool
     */
    public static function isLogin()
    {
        $user = Sentry::getUser();
        if (empty($user))
            return false;
        return true;
    }
    
    /**
     * check active user
     * @author Thuan Truong
     */
    public static function isActive() {
        $user = Sentry::getUser();
        if (!empty($user)) {
            if ($user->activated == 1)
                return true;
            return false;
        }
        return false;
    }
    
    /**
     * get role for user
     * @author Thuan Truong
     * @return int 
     */
    public static function getRole() {
        $user = Sentry::getUser();
        if (empty($user)) {
            return View::make('home');
        }
        
        $groups = $user->getGroups();
        return $groups[0]->id;
    }

}
